package edu.sysu.cshare.util.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import edu.sysu.cshare.control.ClassManager;
import edu.sysu.cshare.control.UserManager;
import edu.sysu.cshare.model.Course;
import edu.sysu.cshare.util.HibernateSessionFactory;

public class Table_Course extends JTable {
	ClassManager classManager;
	UserManager userManager;

	public Table_Course(TableModel dm, ClassManager classManager,
			UserManager userManager) {
		super(dm);
		this.classManager = classManager;
		this.userManager = userManager;

		// 排序
		this.setAutoCreateRowSorter(true);

		// 隐藏第一列(id)
		this.getColumnModel().getColumn(0).setMaxWidth(0);
		this.getColumnModel().getColumn(0).setMinWidth(0);
		this.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
		this.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);

		// GUI.FitTableColumns(this);

		this.setRowHeight(20);
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int row = rowAtPoint(e.getPoint());
				int column = columnAtPoint(e.getPoint());

				if (row < 0)
					return;
				Course course = ClassManager.getCourseById((Long) getValueAt(
						row, 0));
				if (column == 1) {
					if ((Boolean) getValueAt(row, column)) {
						Table_Course.this.userManager.quitCourse(course);
						JOptionPane.showMessageDialog(null, "退课成功");
					} else if (!(Boolean) getValueAt(row, column)) {
						Table_Course.this.userManager.selectCourse(course);
						JOptionPane.showMessageDialog(null, "选课成功");
					}
				}
			}
		});
	}
}
